package com.jingtai.shop.service.user.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jingtai.common.enums.CollectStatusEnum;
import com.jingtai.common.enums.DeleteFlagEnum;
import com.jingtai.common.webfacade.error.CommonException;
import com.jingtai.core.entity.member.MemberCollectGoods;
import com.jingtai.core.entity.member.MemberCollectStore;
import com.jingtai.core.entity.merchants.UserStore;
import com.jingtai.core.entity.shop.MerchantsGoods;
import com.jingtai.core.mapper.member.MemberCollectGoodsMapper;
import com.jingtai.shop.service.goods.MerchantsGoodsService;
import com.jingtai.shop.service.goods.UserStoreService;
import com.jingtai.shop.service.user.MemberCollectGoodsService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class MemberCollectGoodsServiceImpl extends ServiceImpl<MemberCollectGoodsMapper, MemberCollectGoods> implements MemberCollectGoodsService {

    @Resource
    private MemberCollectGoodsService collectGoodsService;
    @Resource
    private UserStoreService userStoreService;
    @Resource
    private MerchantsGoodsService merchantsGoodsService;

    /**
     * 用户查看自己收藏的商品列表
     */
    @Override
    public Page<MemberCollectGoods> showCollectGoods(Long userId,String goodsTitle, Long pageNo, Long pageSize) {
        return collectGoodsService.page(new Page<>(pageNo, pageSize), Wrappers.lambdaQuery(MemberCollectGoods.class)
                .eq(MemberCollectGoods::getUserId, userId)
                .like(StringUtils.isNotBlank(goodsTitle),MemberCollectGoods::getGoodsPicture,goodsTitle)
                .eq(MemberCollectGoods::getIsCollect, CollectStatusEnum.COLLECTED.getStatus())
                .eq(MemberCollectGoods::getDeleteFlag, DeleteFlagEnum.NOT_DELETE.getCode()));
    }

    /**
     * 点击收藏/取消商品
     */
    @Override
    public boolean collectGoods(String goodsCode, Long userId, Integer collectStatus) {
        MerchantsGoods merchantsGoods = merchantsGoodsService.getOne(Wrappers.lambdaQuery(MerchantsGoods.class)
                .eq(MerchantsGoods::getGoodsCode, goodsCode)
                .eq(MerchantsGoods::getDeleteFlag, DeleteFlagEnum.NOT_DELETE.getCode()));
        if (ObjectUtil.isEmpty(merchantsGoods)){
            throw new CommonException("暂未查到该商品!");
        }
        //查询历史收藏记录
        MemberCollectGoods one = collectGoodsService.getOne(Wrappers.lambdaQuery(MemberCollectGoods.class).eq(MemberCollectGoods::getGoodsCode, goodsCode).eq(MemberCollectGoods::getUserId, userId));
        if (ObjectUtil.isEmpty(one)){
            MemberCollectGoods memberCollectGoods = new MemberCollectGoods();
            memberCollectGoods.setUserId(userId);
            memberCollectGoods.setGoodsId(merchantsGoods.getId());
            memberCollectGoods.setGoodsCode(merchantsGoods.getGoodsCode());
            memberCollectGoods.setGoodsListTitle(merchantsGoods.getGoodsListTitle());
            memberCollectGoods.setGoodsPicture(merchantsGoods.getMainFigure());
            memberCollectGoods.setGoodsPrice(merchantsGoods.getGoodsPrice());
            memberCollectGoods.setIsCollect(collectStatus);
            return collectGoodsService.save(memberCollectGoods);
        }else {
            one.setIsCollect(collectStatus);
            return collectGoodsService.updateById(one);
        }
    }
}
